﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UI
{
    public class FileContent  
    {
        public string FileName { get; private set;}
        
        public string FileHeader { get; private set; }
        public List<List<string>> Columns { get; private set; }
        string[] content;

        public static bool IsNumeric(List<string> Column)
        {
            for (int s = 1; s < Column.Count; s++ )
            {
                double r;
                if (double.TryParse(Column[s], out r) == false) return false;
            }
            return true;
        }

        public string GetTerm(int RowNumber, int Columncount)
        {
            return content[RowNumber].Split((char[])null, System.StringSplitOptions.RemoveEmptyEntries)[Columncount];
        }
        public string GetTerm(int RowNumber, string ColumnHeader)
        {
            return content[RowNumber].Split((char[])null, System.StringSplitOptions.RemoveEmptyEntries)[ColumnNumber(ColumnHeader)];
        }
        public string GetTerm(string RowHeader, string ColumnHeader)
        {
            foreach (string line in content)
            {
                string[] terms = line.Split((char[])null, System.StringSplitOptions.RemoveEmptyEntries);

                if (terms[0] == RowHeader)
                {
                    return terms[ColumnNumber(ColumnHeader)];
                }
            }
            throw new System.Exception("Cannot find \t" + RowHeader + " " + ColumnHeader + " in " + FileName);
        }
        public string GetLineThatContains(string term)
        {
            foreach (string line in content)
            {
                if (line.Contains(term))
                {
                    return line;
                }
            }
            return null;
        }
        public bool FileContains(string term)
        {
            if (GetLineThatContains(term) != null) return true;
            return false;
        }
        public string GetLine(int row)
        {
            return content[row];
        }

        public static string[] ReplaceLine(string FileName, string oldline, string newline)
        {
            bool FoundLine = false;
            List<string> newcontent = new List<string>();
            foreach (string line in System.IO.File.ReadAllLines(FileName))
            {
                if (line == oldline)
                {
                    newcontent.Add(newline);
                    FoundLine = true;
                }
                else newcontent.Add(line);
            }

            if (!FoundLine) throw new System.Exception("Cannot find line " + oldline + " in " + FileName);

            return newcontent.ToArray();
        }
        public bool HasColum(string header)
        {
            for (int t = 0; t < Headers.Count(); t++)
            {
                if (Headers[t] == header) return true;
            }
            return false;
        }
        public int ColumnNumber( string header)
        {
            for (int t = 0; t < Headers.Count(); t++)
            {
                if (Headers[t] == header) return t;
            }
            throw new System.Exception("Cannot find header\t" + header + " in " + FileHeader);
        }
        public string[] Headers
        {
            get
            {
                return FileHeader.Split(new char[] { ',', '\t' }, System.StringSplitOptions.RemoveEmptyEntries);
            }
        }
        
        int HeaderNumber(string header)
        {
            for (int h = 0; h < Headers.Length; h++)
            {
                if (Headers[h].Contains(header)) return h;
            }
            throw new System.Exception("Cannot find header " + header);
        }
        public T[] GetUnroundedYears<T>()
        {
            if (HasColum("Year") == false || HasColum("Month") == false) return null;

            List<T> UnroundedYears = new List<T>();
            double[] Years = GetColumn<double>("Year");
            double[] Months = GetColumn<double>("Month");

            for (int v = 1; v < Years.Count(); v++)
            {
                double year = Years[v] + (Months[v] / 12.0);
                UnroundedYears.Add((T)Convert.ChangeType(year, typeof(T))); 
            }
            return UnroundedYears.ToArray();
               
        }
        
        public T[] GetColumn<T>(string header)
        {
            foreach (List<string> column in Columns)
            {
                if (column[0].Trim() == header)
                {
                    List<T> values = new List<T>();
                    for (int i = 0; i < column.Count(); i++ )
                    {
                        try
                        {
                            values.Add((T)Convert.ChangeType(column[i], typeof(T)));

                        }
                        catch 
                        {
                            if (i > 1)
                            {
                                return null;
                            }
                        }
                    }
                    return   values.ToArray();
                }
            }
            throw new System.Exception("Cannot find a column with header " + header +" "+ this.FileName);

            
        }
        public void Write()
        {
            System.IO.File.WriteAllLines(FileName, content);
        }

        private void SetColumns()
        {
            Columns = new List<List<string>>();
            for (int col = 0; col < Headers.Count(); col++)
            {
                Columns.Add(new List<string>());
            }
            foreach (string line in content)
            {
                string[] values = line.Split(new char[] { ',', '\t' }, System.StringSplitOptions.RemoveEmptyEntries);
                for (int col = 0; col < Headers.Count(); col++)
                {
                    Columns[col].Add(values[col]);
                }
            }
        }


        public FileContent(string FileName)
        {
            this.FileName = FileName;
            
            content = new List<string>(System.IO.File.ReadAllLines(this.FileName)).Where(s => !string.IsNullOrEmpty(String.Join(null, s.Split('\t')))).Distinct().ToArray();

            this.FileHeader = GetLine(0);

            SetColumns();

            return;
        }
        
        
        
    }
}
